package com.jz.beijianbao.planclass.service.impl;

import com.jz.beijianbao.planclass.bean.PlanClass;
import com.jz.beijianbao.planclass.bean.PlanClassAdd;
import com.jz.beijianbao.planclass.bean.PlanClassSelectVo;
import com.jz.beijianbao.planclass.enumclass.StatusEnum;
import com.jz.beijianbao.planclass.mapper.PlanMapper;
import com.jz.beijianbao.planclass.service.PlanService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PlanServiceImpl implements PlanService {

    @Autowired
    private PlanMapper planClasses;

    @Override
    public ResultVO queryPag(PlanClassSelectVo planClassSelectVo) {
        //数据
        List<PlanClass> subClasses = planClasses.queryPag(planClassSelectVo);
        for (PlanClass sub:subClasses) {
            for (StatusEnum senum:StatusEnum.values()) {
                if (sub.getStatus()==senum.getStatus()){
                    sub.setStatusName(senum.getStatusName());
                }
            }
        }
        //数量
        int count = planClasses.queryCount(planClassSelectVo);
        //返回
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(subClasses);
        allRecordsVo.setPageIndex(planClassSelectVo.getPageIndex());
        allRecordsVo.setPageSize(planClassSelectVo.getPageSize());
        allRecordsVo.setTotalPage(allRecordsVo.countPage(count));
        allRecordsVo.setTotalSize(count);
        return ResultBuildVO.success(allRecordsVo);
    }

    @Override
    public ResultVO add(PlanClassAdd planClassAdd) {
        PlanClassAdd code = planClasses.queryByCode(planClassAdd.getCode());
        if (code!=null){
            return ResultBuildVO.error("编码已经存在");
        }
        PlanClassAdd name = planClasses.queryByName(planClassAdd.getName());
        if (name!=null){
            return ResultBuildVO.error("名称已经存在");
        }
        Date date = new Date();
        PlanClass subClass = new PlanClass();
        BeanUtils.copyProperties(planClassAdd,subClass);
        subClass.setCreateTime(date);
        subClass.setCreateName("通过session获取用户名称");
        int add = planClasses.add(subClass);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO selectById(Integer id) {
        PlanClass planClass = planClasses.selectById(id);
        if (planClass.getStatus()==1){
            return ResultBuildVO.error("启用状态不能修改");
        }
        if (planClass==null){
            return ResultBuildVO.error("该大件不存在");
        }else {
            return   ResultBuildVO.success();
        }

    }

    @Override
    public ResultVO update(PlanClassAdd planClassAdd) {
        PlanClassAdd name = planClasses.queryByName(planClassAdd.getName());
        if (name!=null){
            return ResultBuildVO.error("名称已经存在");
        }
        planClasses.update(planClassAdd);
        return   ResultBuildVO.success();
    }

    @Override
    public ResultVO status(PlanClass planClass) {
        planClasses.status(planClass);
        return   ResultBuildVO.success();
    }
}
